using System;
using System.Collections.Generic;
using System.Text;
using Zoo;
using System.ComponentModel;


namespace Zoo
{
    
    public partial class DefaultController
    {
        private IDefaultView _view;
        private IZooModel _zooModel;
        const int MaxLoginAttemps = 3;

        public DefaultController(IDefaultView defaultView,IZooModel zooModel)
        {
            _view = defaultView;
            _zooModel = zooModel;
        }

        public void Initialize()
        { 
            _view.PressLogin += new EventHandler(DefaultView_PressLogin);
            _view.AuthenticatedBoxVisible = _view.User.Identity.IsAuthenticated;
            _view.LoginBoxVisible = !_view.AuthenticatedBoxVisible;


                if(_view.LoginBoxVisible)
                    _view.LoginName = _view.CookieLoginName;
            
                _view.NewUsers = _zooModel.GetNewUsers(_view.Language);
            
            

        }

        public void DefaultView_PressLogin(object sender, EventArgs e)
        {
            int userID=_zooModel.GetUserID(_view.LoginName, _view.Password);
            if (userID > 0  && _view.MissedLoginAttempts<3)
            {
                _view.InvalidLoginMessageVisible = false;
                _view.SetAuthentication(userID);
                _view.LoginBoxVisible = false;
                _view.AuthenticatedBoxVisible = true;
            }
            else
            {
                _view.MissedLoginAttempts++;
                _view.InvalidLoginMessageVisible = true;
                _view.LoginBoxVisible = _view.MissedLoginAttempts < MaxLoginAttemps;
                _view.Max3AuthenticatedMessageVisible = _view.MissedLoginAttempts > MaxLoginAttemps -1;
            }
        }
    }
}
